package com.irdstudio.efp.esb.service.client;

import com.irdstudio.basic.framework.core.util.SpringContextUtils;
import com.irdstudio.efp.esb.service.client.impl.DefaultESBConfig;
import com.irdstudio.efp.esb.service.client.impl.ESBClient;
import com.irdstudio.efp.esb.service.client.impl.ReferenceESBBeanCreator;
import com.irdstudio.efp.esb.service.client.impl.ReferenceESBClientImpl;

import static java.util.Objects.requireNonNull;

/**
 * 需求编号:2019D08--
 * 问题编号:
 * 开发人员: caoxin
 * 创建日期:2019/9/27
 * 功能描述:ESBRequestBuilder模仿实现
 * 修改日期:2019/9/27
 * 修改描述:
 */
public class ESBRequestBuilder {

    public static Builder buildClient() {
        return new Builder();
    }

    public static class Builder {

        private Object bodyBean;
        private String tradeNo;
        private String scene;
        private EsbBaseBean esbBaseBean;


        private ESBConfig config = SpringContextUtils.getBean(DefaultESBConfig.class);
        private HttpClientFactory factory = SimpleHttpClientFactory.newInstance(config.getTimeout());
        private REsbBeanCreator esbBeanCreator = ReferenceESBBeanCreator.getINSTANCE();


        public Builder withBody(Object object) {
            bodyBean = requireNonNull(object);
            return this;
        }

        public Builder withTradeNo(String tradeNo) {
            this.tradeNo = requireNonNull(tradeNo);
            return this;
        }

        public Builder withScene(String scene) {
            this.scene = requireNonNull(scene);
            return this;
        }

        public Builder withConfig(ESBConfig config) {
            this.config = requireNonNull(config);
            return this;
        }

        public Builder withEsbBaseBean(EsbBaseBean esbBaseBean) {
            this.esbBaseBean = requireNonNull(esbBaseBean);
            return this;
        }

        public Builder withHttpClientFactory(HttpClientFactory factory) {
            this.factory = requireNonNull(factory);
            return this;
        }

        public Builder withESBBeanFactory(REsbBeanCreator esbBeanCreator) {
            this.esbBeanCreator = requireNonNull(esbBeanCreator);
            return this;
        }

        public ESBClient create() {
            return new ReferenceESBClientImpl(this);
        }

        /*
         * getters
         * */
        public Object getToSend() {
            return bodyBean;
        }

        public String getTradeNo() {
            return tradeNo;
        }

        public String getScene() {
            return scene;
        }

        public ESBConfig getConfig() {
            return config;
        }

        public REsbBeanCreator getESBBeanFactory() {
            return esbBeanCreator;
        }

        public HttpClientFactory getFactory() {
            return factory;
        }

		public EsbBaseBean getEsbBaseBean() {
			return esbBaseBean;
		}
	}

    private ESBRequestBuilder() {
        throw new IllegalAccessError();
    }
}
